package Move;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class JLableMoveDemo extends JFrame {
    public JLableMoveDemo() throws Exception {
        super();
        ImageIcon ii = new ImageIcon("images/menu_bg.png");
        GrabAndScrollLabel label = new GrabAndScrollLabel(ii);
        label.setPreferredSize(new Dimension(1400, 700));
        label.setPreferredSize(new Dimension(1400, 700));

        JScrollPane jsp = new JScrollPane(label);
        getContentPane().add(jsp);
        // 设置滚动条隐藏
        jsp.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        setSize(800, 600);
        setVisible(true);

        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        addWindowListener(l);
    }

    public static void main(String[] args) throws Exception {
        new JLableMoveDemo();
    }
}

class GrabAndScrollLabel extends JLabel {
    public GrabAndScrollLabel(ImageIcon i) {
        super(i);

        MouseInputAdapter mia = new MouseInputAdapter() {
            int xDiff = 700, yDiff = 350;

            Container c;

            public void mouseDragged(MouseEvent e) {
//                c = GrabAndScrollLabel.this.getParent();
//                if (c instanceof JViewport) {
//                    JViewport jv = (JViewport) c;
//                    Point p = jv.getViewPosition();
//                    int newX = p.x - (e.getX() - xDiff);
//                    int newY = p.y - (e.getY() - yDiff);
//
//                    int maxX = GrabAndScrollLabel.this.getWidth()
//                            - jv.getWidth();
//                    int maxY = GrabAndScrollLabel.this.getHeight()
//                            - jv.getHeight();
//                    if (newX < 0)
//                        newX = 0;
//                    if (newX > maxX)
//                        newX = maxX;
//                    if (newY < 0)
//                        newY = 0;
//                    if (newY > maxY)
//                        newY = maxY;
//
//                    jv.setViewPosition(new Point(newX, newY));
//                }
            }

            public void mousePressed(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
//                xDiff = e.getX();
//                yDiff = e.getY();
                System.out.println(xDiff + ", "+yDiff);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                c = GrabAndScrollLabel.this.getParent();
                if (c instanceof JViewport) {
                    JViewport jv = (JViewport) c;
                    Point p = jv.getViewPosition();
                    int newX = p.x + (e.getX() - xDiff);
                    int newY = p.y + (e.getY() - yDiff);

                    int maxX = GrabAndScrollLabel.this.getWidth()
                            - jv.getWidth();
                    int maxY = GrabAndScrollLabel.this.getHeight()
                            - jv.getHeight();
                    if (newX < 0)
                        newX = 0;
                    if (newX > maxX)
                        newX = maxX;
                    if (newY < 0)
                        newY = 0;
                    if (newY > maxY)
                        newY = maxY;

                    jv.setViewPosition(new Point(newX, newY));
                }
            }

            public void mouseReleased(MouseEvent e) {
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        };
        addMouseMotionListener(mia);
        addMouseListener(mia);
    }
}
